package com.sgb.kjwl.view.ui.adapter;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bm.library.PhotoView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.kjwl.R;

import java.util.List;

/**
 * @author zkw
 * @describe
 * @date
 */
public class PhotoPagerAdapter extends PagerAdapter {
    private Context context;
    private List<String> imgUrl;


    public PhotoPagerAdapter(Context context, List<String> imgUrl) {
        this.context = context;
        this.imgUrl = imgUrl;
    }

    @Override
    public int getCount() {
        return imgUrl == null ? 0 : imgUrl.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.lb_photo_item, null);
        PhotoView phoneView = view.findViewById(R.id.photo_view);
        phoneView.enable();
        RequestOptions options = new RequestOptions()
                .fitCenter();
        Glide.with(context)
                .load(imgUrl.get(position).trim())
                .apply(options)
                .into(phoneView);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
